<?php

namespace App\Http\Controllers\Develop;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\User\Module;
use App\Models\User\ModuleStyle;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis as Redis;

class ModuleStyleController extends Controller
{
    public function index()
    {
        return view ('develop/moduleStyleList');
    }

    //列表
    public function search(Request $request)
    {
        $where = [];
        $style_name = $request->input('style_name');

        if (!empty($style_name)) {
            $where[] = ['style_name',$style_name];
        }

        $style_data = ModuleStyle::where($where)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $return = [
            'total' => $style_data['total'],
            'rows' => []
        ];

        if ( $style_data['data'] ) {

            foreach( $style_data['data'] as $row ){

                //查询模块类型
                $style_obj = Module::find($row['module_id']);

                if ($style_obj['attributes']['module_type'] == 1){
                    $module_type = '商品';
                }else{
                    $module_type = '图片';
                }
                $operation ='<a href="javascript: void(0);" onclick="Module.edit(' . $row['id'] . ',2)">修改</a>&nbsp;&nbsp;
                             <a href="javascript: void(0);" onclick="Module.del(' . $row['id'] . ')">删除</a>&nbsp;&nbsp;';

                $return['rows'][] = [
                    'operation' => $operation,
                    'style_name' => $row['style_name'],
                    'module_name' => $style_obj['attributes']['module_name'],
                    'module_type' => $module_type
                ];
            }
        }

        return response()->json($return);

    }

    //添加，修改样式库信息
    public function edit (Request $request)
    {

        //获取模块名称数据
        $style  = Module::all();

        //按钮入口标识(样式库列表添加按钮)
        $remark = 0;

        //模块列表添加样式库
        if ($request['flag'] == 1){
            $module_data= Module::find($request['id']);
            if($module_data){
                $module_array = $module_data['attributes'];
                //按钮入口标识(模块列表添加样式库按钮)
                $remark = 2;
            }
        }

        //样式库列表修改样式库
       if ( $request['flag'] == 2){
           if ( $request['id'] >0) {
               $module_obj = ModuleStyle::find($request['id']);
               if ( !$module_obj) {
                   return response()->json(['code' => 10000, 'message' => '模块信息没有找到']);
               }
               $module = $module_obj['attributes'];
               if ($module){
                   //按钮入口标识(样式库列表修改按钮)
                   $remark = 1;
               }
           }
       }

        //返回数据
        $data = array(
            'style_data' =>$style,
            'module_array' =>isset($module_array) ? $module_array: '',
            'style' => isset($module) ? $module: '',
            'id' => $request['id'],
            'remark'=>$remark
        );

        return view ('develop/moduleStyleEdit',$data);
    }

    //保存样式库信息
    public function store(Request $request)
    {
        $module_name = $request->input('module_name');
        $style_name = $request->input('style_name');
        $style_css = $request->input('style_css');
        $id = $request->input('id');

        if ( empty($module_name) ) {
            return response()->json(array('code' => 100001, 'message' => '所属模块不能为空'));
        }
        if ( empty($style_name) ) {
            return response()->json(array('code' => 100005, 'message' => '默认样式库名称不能为空'));
        }
        if ( empty($style_css) ) {
            return response()->json(array('code' => 100006, 'message' => '默认样式库CSS不能为空'));
        }

        $style = ModuleStyle::find($id);
        if (!$style) {
            $style = new ModuleStyle();
            $style->uuid = makeUuid();
            $style->timeStamp = Carbon::now();
            $style->creator = Redis::get('WDH_ADMIN_USER_ID_' . session_id());
            $style->createTime = Carbon::now();
        }
        $style->module_id = $module_name;
        $style->style_name = $style_name;
        $style->style_css = $style_css;
        $style->save();

        return response()->json(['code' => 200, 'message' => '保存成功']);
    }

    //查看样式库信息
    public function detail($id)
    {
        $style = ModuleStyle::where(['module_id'=>$id])->get()->toArray();

        return view ('develop/moduleStyleDetail',['style'=>$style]);

    }

    //删除样式库信息
    public function delete($id)
    {
        $style = ModuleStyle::find($id);
        if ( $style ) {
            $style->delete();
        }

        return response()->json(['code' => 200, 'message' => '删除成功']);

    }

}
